package minh.bac.wcbrazil2014.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import minh.bac.wcbrazil2014.R;
import minh.bac.wcbrazil2014.WCTAG;
import minh.bac.wcbrazil2014.object.MatchScheduleObj;
import minh.bac.wcbrazil2014.object.VongBangObj;
import minh.bac.wcbrazil2014.object.VongDau;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class VongBangAdapter extends BaseExpandableListAdapter {

	Context context;
	ArrayList<String> dataHeader;
	HashMap<String, ArrayList<MatchScheduleObj>> hashMap;
	DisplayImageOptions displayImageOptions;
	int typeShow;
	HashMap<String, ArrayList<VongDau>> hashMapVongDau;

	public VongBangAdapter(Context context, ArrayList<String> dataHeader,
			HashMap<String, ArrayList<MatchScheduleObj>> hashMap,
			DisplayImageOptions displayImageOptions, int typeShow) {
		this.context = context;
		this.hashMap = hashMap;
		this.dataHeader = dataHeader;
		this.displayImageOptions = displayImageOptions;
		this.typeShow = typeShow;
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return dataHeader.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return hashMap.get(dataHeader.get(groupPosition)).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return dataHeader.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return hashMap.get(dataHeader.get(groupPosition)).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ViewHolderGroup holderGroup;
		if (convertView == null) {
			holderGroup = new ViewHolderGroup();
			convertView = inflater.inflate(R.layout.grouplistview_layout,
					parent, false);
			holderGroup.groupTextView = (TextView) convertView
					.findViewById(R.id.itemgroup_name);
			convertView.setTag(holderGroup);
		} else
			holderGroup = (ViewHolderGroup) convertView.getTag();
		String key = (String) getGroup(groupPosition);
		String[] tempS = key.split(",");
		key = tempS[1].trim() + ", " + tempS[0];
		holderGroup.groupTextView.setText(key);
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		ViewHolderChild holderChild;
		if (convertView == null) {
			holderChild = new ViewHolderChild();
			convertView = inflater.inflate(R.layout.itemchild_listview, parent,
					false);
			//

			holderChild.bang = (TextView) convertView
					.findViewById(R.id.bangdau_itemlistview);
			holderChild.doi1 = (TextView) convertView
					.findViewById(R.id.doi1_itemlistview);
			holderChild.time = (TextView) convertView
					.findViewById(R.id.time_itemlistivew);
			holderChild.doi2 = (TextView) convertView
					.findViewById(R.id.doi2_itemlistview);
			holderChild.imageDoi1 = (ImageView) convertView
					.findViewById(R.id.image_doi1);
			holderChild.imageDoi2 = (ImageView) convertView
					.findViewById(R.id.image_doi2);
			holderChild.scoredoi1 = (TextView) convertView.findViewById(R.id.score_doi1);
			holderChild.scoredoi2 = (TextView) convertView.findViewById(R.id.score_doi2);
			//
			convertView.setTag(holderChild);
		} else
			holderChild = (ViewHolderChild) convertView.getTag();
		if (typeShow == WCTAG.TYPE_SHOW_VONGBANG) {
			holderChild.bang.setText("Bảng "
					+ ((VongBangObj) hashMap.get(dataHeader.get(groupPosition))
							.get(childPosition)).getBangDau());
		} else
			holderChild.bang.setText("Bảng "
					+ ((VongDau) hashMap.get(dataHeader.get(groupPosition))
							.get(childPosition)).getVong());

		holderChild.doi1.setText(hashMap.get(dataHeader.get(groupPosition))
				.get(childPosition).getDoi1());
		holderChild.doi2.setText(hashMap.get(dataHeader.get(groupPosition))
				.get(childPosition).getDoi2());
		holderChild.time.setText(hashMap.get(dataHeader.get(groupPosition))
				.get(childPosition).getGio());
		String score1, score2;
		score1 = hashMap.get(dataHeader.get(groupPosition)).get(childPosition).getScore1();
		score2 = hashMap.get(dataHeader.get(groupPosition)).get(childPosition).getScore2();
		if(score1 == null)
		{
			score1 = "-";
		}
		if( score2 == null)
		{
			score2 = "-";
		}
		holderChild.scoredoi1.setText(score1);
		holderChild.scoredoi2.setText(score2);
		String urlImgDoi1, urlImgDoi2;
		urlImgDoi1 = hashMap.get(dataHeader.get(groupPosition))
				.get(childPosition).getImgLinkDoi1();
		urlImgDoi2 = hashMap.get(dataHeader.get(groupPosition))
				.get(childPosition).getImgLinkdoi2();
		if (urlImgDoi1 == null)
			urlImgDoi1 = "";
		if (urlImgDoi2 == null)
			urlImgDoi2 = "";
		Log.d("ADAPTER", urlImgDoi1 + "   " + urlImgDoi2);
		// load image
		ImageLoader.getInstance().displayImage(urlImgDoi1,
				holderChild.imageDoi1, displayImageOptions);
		ImageLoader.getInstance().displayImage(urlImgDoi2,
				holderChild.imageDoi2, displayImageOptions);
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	class ViewHolderGroup {
		TextView groupTextView;
	}

	class ViewHolderChild {

		TextView bang, doi1, time, doi2, scoredoi1,scoredoi2;
		ImageView imageDoi1, imageDoi2;
	}

}
